package com.example.test0520.activity;

import android.content.Intent;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import androidx.annotation.RequiresApi;
import com.example.test0520.MainActivity;
import com.example.test0520.R;
import com.example.test0520.util.City;
import org.litepal.LitePal;
import java.util.List;

public class AddActivity extends BaseActivity {

    private ImageView backImg;
    private RelativeLayout currentCity;
    private ListView addList;
    private LinearLayout addCity;
    private List<City> all;
    private TextView upDate;
    private MyAdapter myAdapter;
    private Boolean a = false;


    @Override
    protected int initLayout() {
        return R.layout.activity_add;
    }
    @Override
    protected void initView() {
        backImg = (ImageView) findViewById(R.id.back_img);
        currentCity = (RelativeLayout) findViewById(R.id.current_city);
        addList = (ListView) findViewById(R.id.add_list);
        addCity = findViewById(R.id.add_city);
        upDate = findViewById(R.id.update_text);

    }

    @Override
    protected void initData() {
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        all = LitePal.findAll(City.class);
        myAdapter = new MyAdapter();
        addList.setAdapter(myAdapter);
        setListener();
    }
    private void setListener() {
        currentCity.setOnClickListener(v -> {
            Intent intent = new Intent(AddActivity.this, MainActivity.class);
            intent.putExtra("pos", 0);
            startActivity(intent);
        });
        backImg.setOnClickListener(v -> onBackPressed());
        addCity.setOnClickListener(v -> startActivity(new Intent(AddActivity.this, QueryActivity.class)));
        addList.setOnItemClickListener((parent, view, position, id) -> {
            Intent intent = new Intent(AddActivity.this, MainActivity.class);
            intent.putExtra("pos", position);
            startActivity(intent);
        });
        upDate.setOnClickListener(v -> {
            if (a){
//                    myAdapter.listAdd.setVisibility(View.GONE);
                upDate.setText("编辑列表");
                a = false;
            }else{
//                    myAdapter.listAdd.setVisibility(View.VISIBLE);
                upDate.setText("保存编辑");
                a = true;
            }
            myAdapter.notifyDataSetChanged();
        });
    }



    class MyAdapter extends BaseAdapter {

        private TextView cityListText;
        private TextView provinceListText;
        private ImageView listAdd;

        @Override
        public int getCount() {
            return all.size();
        }

        @Override
        public Object getItem(int position) {
            return all.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @RequiresApi(api = Build.VERSION_CODES.P)
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v;
            if (convertView == null) {
                v = LayoutInflater.from(getApplicationContext()).inflate(R.layout.city_list_item, parent, false);
            } else {
                v = convertView;
            }
            initView(v);
            if (!a) {
                listAdd.setVisibility(View.GONE);
            }else{
                listAdd.setVisibility(View.VISIBLE);
                listAdd.setOnClickListener(v1 -> {
                    all.remove(position);
                    LitePal.deleteAll(City.class);
                    for (City c :
                            all) {
                        new City(c.getProvince(), c.getCity(), c.getDistrict()).save();
                    }
                    all = LitePal.findAll(City.class);
                    myAdapter.notifyDataSetChanged();
                });
            }
            if (all.size() != 0) {
                if (all.get(position).getCity().equals(all.get(position).getDistrict())) {
                    cityListText.setText(all.get(position).getCity() + "市");
                    provinceListText.setText(all.get(position).getProvince());
                } else {
                    cityListText.setText(all.get(position).getDistrict());
                    provinceListText.setText(all.get(position).getProvince() + " " + all.get(position).getCity());
                }
            }
            return v;
        }

        private void initView(View v) {
            cityListText = (TextView) v.findViewById(R.id.city_list_text);
            provinceListText = (TextView) v.findViewById(R.id.province_list_text);
            listAdd = v.findViewById(R.id.list_add);
        }
    }
}